/*
 * @(#)EditStageForWorkflowStepAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.MtmWfStageWfStep;
import com.dcivision.workflow.bean.WorkflowGroup;
import com.dcivision.workflow.bean.WorkflowRecord;
import com.dcivision.workflow.dao.MtmWfStageWfStepDAObject;
import com.dcivision.workflow.dao.WorkflowGroupDAObject;
import com.dcivision.workflow.dao.WorkflowRecordDAObject;
import com.dcivision.workflow.dao.WorkflowStageDAObject;
import com.dcivision.workflow.dao.WorkflowStepDAObject;



/**
  EditStageForWorkflowStepAction.java

  This class is for maint workflow tracking .

  @author          dick xie
  @company         DCIVision Limited
  @creation date   29/07/2005
  @version         $Revision: 1.10 $
*/
public class EditStageForWorkflowStepAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.10 $";
  private final static String MESSAGE_RECORD_INSERT = "common.message.record_inserted";

  /** Creates a new instance of EditStageForWorkflowStepAction */
  public EditStageForWorkflowStepAction() {
    super();
    this.setListName("workflowStageList");
    
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_RECORD);
  }
  
  public String getMajorDAOClassName() {
    return("com.dcivision.customize.workflow.dao.WorkflowStageDAObject");
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    AbstractSearchForm searchForm = (AbstractSearchForm)form;
    String workflowRecordID = request.getParameter("workflowRecordID");
    ListWorkflowStageForm StageForm = (ListWorkflowStageForm) form;
    //add by sars
    if(StageForm.getNavMode().length()>3&&StageForm.getNavMode().substring(0,3).equals("LLL"))
    {
    	request.setAttribute("groupName", StageForm.getNavMode().split(",")[1]);
        request.setAttribute("recordName", StageForm.getNavMode().split(",")[2]);
        StageForm.setNavMode("");
    }
    String action = StageForm.getAction();
    if (action != null && action.equalsIgnoreCase("searchStage")) {
      ListWorkflowStageForm oldSearchForm=(ListWorkflowStageForm)request.getSession().getAttribute("oldSearchForm"); 
      if(oldSearchForm==null){
        request.getSession().setAttribute("oldSearchForm", StageForm);
      }
      StageForm.setAction(null);
    }
    ListWorkflowStageForm listWorkflowStageForm = (ListWorkflowStageForm)searchForm;
    if (!Utility.isEmpty(workflowRecordID) && Utility.isEmpty(listWorkflowStageForm.getWorkflowRecordID())) {
    	listWorkflowStageForm.setWorkflowRecordID(workflowRecordID);
    }
    WorkflowStageDAObject workflowStageDAO = new WorkflowStageDAObject(this.getSessionContainer(request), this.getConnection(request));
    MtmWfStageWfStepDAObject mtmWfStageDAO = new MtmWfStageWfStepDAObject(this.getSessionContainer(request), this.getConnection(request));
    log.info(this.getClass().getName()+", OP Mode:"+searchForm.getOpMode()+", Nav Mode:"+searchForm.getNavMode());
//  Set the function code for current action.
    
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }

    if (Utility.isEmpty(searchForm.getNavMode()) && GlobalConstant.OP_MODE_UPDATE.equals(searchForm.getOpMode())){
    	String workflowStageIDs = request.getParameter("workflowStageIDs");
    	if (!Utility.isEmpty(workflowStageIDs)){
    		String[] workflowStageID=TextUtility.splitString(workflowStageIDs, "|");
    		if (workflowStageID.length > 0) {
    			for (int i = 0; i < workflowStageID.length; i ++) {
    				String[] curWorkflowStageID=TextUtility.splitString(workflowStageID[i], ":");
    				if(!Utility.isEmpty(curWorkflowStageID[0])) {
    					List mtmWfStageWfStepList = mtmWfStageDAO.getListByWorkflowStepID(new Integer(curWorkflowStageID[0]));
    					String flag = "Y";
    					for (int j=0; !Utility.isEmpty(mtmWfStageWfStepList) && j<mtmWfStageWfStepList.size();j++) {
    						MtmWfStageWfStep mtmWfStageWfStep = (MtmWfStageWfStep)mtmWfStageWfStepList.get(j);
    						if ((!Utility.isEmpty(curWorkflowStageID[1]) && !mtmWfStageWfStep.getWorkflowStageID().toString().equals(curWorkflowStageID[1]))
    								|| (Utility.isEmpty(curWorkflowStageID[1]) && !Utility.isEmpty(mtmWfStageWfStep.getWorkflowStageID()))){
    							mtmWfStageDAO.physicDelete(mtmWfStageWfStep);
    						} else if ( mtmWfStageWfStep.getWorkflowStageID().toString().equals(curWorkflowStageID[1])){
    							
    							flag = "N";
    						}
    					}
    					if ("Y".equals(flag)) {
                if (!Utility.isEmpty(curWorkflowStageID[1]) && !"-1".equals(curWorkflowStageID[1])) {
    						  MtmWfStageWfStep newWfStep = new MtmWfStageWfStep();
    					  	newWfStep.setWorkflowStepID(new Integer(curWorkflowStageID[0]));
    					  	newWfStep.setWorkflowStageID(new Integer(curWorkflowStageID[1]));
                /*
    						if("1".equals(curWorkflowStageID[2])) {
    							newWfStep.setRecordStatus(GlobalConstant.RECORD_STATUS_PENDING);
								}else if("0".equals(curWorkflowStageID[2])) {
									newWfStep.setRecordStatus(GlobalConstant.RECORD_STATUS_ACTIVE);
								}
                */
								  newWfStep = (MtmWfStageWfStep)mtmWfStageDAO.insertObject(newWfStep);
                }
    					}
    				}
    			}
    		}
    		addMessage(request, MESSAGE_RECORD_INSERT);
    	}
    	commit(request);
    }
	 this.canNew(form, request);
   return super.execute(mapping, form, request, response);
   
  }
  
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    ListWorkflowStageForm listWorkflowStageForm = (ListWorkflowStageForm)form;
  	String navMode = request.getParameter("navMode");
  	String workflowRecordID = request.getParameter("workflowRecordID");
  	if (Utility.isEmpty(workflowRecordID)) {
  		workflowRecordID=(listWorkflowStageForm.getWorkflowRecordID());
  	}
    WorkflowStageDAObject workflowStageDAO = new WorkflowStageDAObject(this.getSessionContainer(request), this.getConnection(request));
    List workflowStepList = workflowStageDAO.getFullWorkflowStepList(listWorkflowStageForm);
    List workflowStageList = workflowStageDAO.getListByWorkflowRecordID(new Integer(workflowRecordID));
    	
    request.setAttribute("workflowStepList", workflowStepList);
    request.setAttribute("workflowStageList", workflowStageList);
  }
  private void canNew(ActionForm form, HttpServletRequest request)
  throws NumberFormatException, ApplicationException {
    AbstractSearchForm searchForm = (AbstractSearchForm) form;
    ListWorkflowStageForm listWorkflowStageForm = (ListWorkflowStageForm) searchForm;
    String groupName = null;
    String recordName = null;
    if (!Utility.isEmpty(listWorkflowStageForm.getWorkflowRecordID())) {
      WorkflowRecordDAObject wfrDAO = new WorkflowRecordDAObject(this
          .getSessionContainer(request), this.getConnection(request));
      recordName = ((WorkflowRecord) wfrDAO.getObjectByID(Integer
          .valueOf(listWorkflowStageForm.getWorkflowRecordID())))
          .getWorkflowCode();
      if (Utility.isEmpty(listWorkflowStageForm.getWorkflowGroupID())) {
        listWorkflowStageForm.setWorkflowGroupID((((WorkflowRecord) wfrDAO
            .getObjectByID(Integer.valueOf(listWorkflowStageForm
                .getWorkflowRecordID()))).getWorkflowGroupID()).toString());
      }
    }
    if (!Utility.isEmpty(listWorkflowStageForm.getWorkflowGroupID())) {
      WorkflowGroupDAObject wfgDAO = new WorkflowGroupDAObject(this
          .getSessionContainer(request), this.getConnection(request));
      groupName = ((WorkflowGroup) wfgDAO.getObjectByID(Integer
          .valueOf(listWorkflowStageForm.getWorkflowGroupID()))).getGroupName();
    }
    request.setAttribute("groupName", groupName);
    if (!Utility.isEmpty(listWorkflowStageForm.getWorkflowRecordID())) {

      request.setAttribute("recordName", recordName);
      MtmWfStageWfStepDAObject mtmStageStepDAO = new MtmWfStageWfStepDAObject(
          this.getSessionContainer(request), this.getConnection(request));
      WorkflowStepDAObject wfStepDAO = new WorkflowStepDAObject(this
          .getSessionContainer(request), this.getConnection(request));
      Integer stepNum = new Integer(wfStepDAO.getListByWorkflowRecordID(
          new Integer(listWorkflowStageForm.getWorkflowRecordID())).size());
      Integer stageStepNum = new Integer(mtmStageStepDAO
          .getListByWorkflowRecordID(
              new Integer(listWorkflowStageForm.getWorkflowRecordID())).size());
      if (!stepNum.equals(stageStepNum)) {
        request.setAttribute("canNew", "Y");
      }
    }
  }
}